package com.hm.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hm.reggie.common.R;
import com.hm.reggie.entity.Orders;
import com.hm.reggie.entity.User;
import com.hm.reggie.service.OrderDetailService;
import com.hm.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private OrderService orderService;

    /***
     * http://localhost:8080/order/submit
     * 参数：
     * {remark: "", payMethod: 1, addressBookId: "1725795608319569921"}
     * 接受方式：
     * @RequestBody Orders orders   实体类接受
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders, HttpSession session){
        orderService.submit(orders,session);
        return R.success("提交成功");
    }

    /***
     * http://localhost:8080/order/userPage?page=1&pageSize=5
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<Orders>> page(Integer page,Integer pageSize,HttpSession session){
        //获取当前用户数据
        User user  = (User)session.getAttribute("user");
        Long userId = user.getId();
        //构造分页对象
        Page<Orders> page1 = new Page<>();
        //构造条件
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,userId);
        //执行查询
        orderService.page(page1, queryWrapper);

        return R.success(page1);
    }

    /***
     * http://localhost:8080/order/userPage?page=1&pageSize=5
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> page2(Integer page,Integer pageSize,HttpSession session){
        //获取当前用户数据
        User user  = (User)session.getAttribute("user");
        //Long userId = user.getId();
        //构造分页对象
        Page<Orders> page1 = new Page<>();
        //构造条件
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //queryWrapper.eq(userId != null,Orders::getUserId,userId);
        //执行查询
        orderService.page(page1, queryWrapper);

        return R.success(page1);
    }
}
